# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html



class ScrapyDemoPipeline(object):

    def __init__(self):
        self.chapters = dict()

    def close_spider(self, spider):
        print('spider ending --------- %s' % spider)
        filename = 'content.txt'
        if 'book_name' in dir(spider) and spider.book_name:
            filename = '%s.txt' % spider.book_name
        total = len(self.chapters.keys())
        keys = self.chapters.keys()
        with open(filename, 'w', encoding='utf-8') as f:
            for i in range(1, total + 1):
                if i in keys:
                    chapter = self.chapters[i]
                    f.write(chapter['title'])
                    f.write('\n')
                    for line in chapter['content']:
                        f.write(line.replace('\xa0', ' '))
                    f.write('\n')

    def process_item(self, item, spider):
        chapter_num = item['chapter_num']
        print('解析完成章节%d' % chapter_num)
        self.chapters[chapter_num] = item
        return item
